clear all 
set more off

* Institutions table, table 2

* main flows data
use "Transitions_EUN_JJ_OO", clear

* need to add country names to the dataset to merge with world bank WDI 
do "CalledFiles/CodeSnippet_AddCountryNames.do"
order country, after(ccode2)


* add World Bank Labor Regulations
replace ccode = "ITA" if ccode == "ITL"
replace ccode = "ROM" if ccode == "ROU"
merge m:1 ccode year using "CalledFiles/WB_EmploymentRegulation_2004_2020_Clean.dta"
keep if _merge == 3 | _merge == 1
drop _merge
drop countrywb 

keep ccode ccode2 country year trans ytrate rich rc_severance1year AGDP lcgdp

tempfile hold1data
save `hold1data'


* get the ratio wage -- only available 2014-2018
use "CalledFiles/cleaned_WB_LaborRegulations_2014_2018.dta"
keep country year h_ratwage
merge 1:m country year using `hold1data'
keep if _merge == 2 | _merge == 3
drop _merge

* clean up
order country ccode ccode2 year

order ccode ccode2 year trans ytrate rc_* h_* rich AGDP lcgdp
sort ccode year trans

label drop _all

* create z score
foreach x in rc_severance1year h_ratwage {
	bys trans: egen _h1 = mean(`x')
	bys trans: egen _h2 = sd(`x')
	gen `x'_z = (`x' - _h1)/_h2
	drop _h1 _h2

	bys trans: egen _h1 = mean(`x') if rich == 1
	bys trans: egen _h2 = sd(`x') if rich == 1
	gen `x'_z_rich = (`x' - _h1)/_h2
	drop _h1 _h2
}


* regressions
local xexit "rc_severance1year_z"
local xjfr "h_ratwage_z"
local xjj "rc_severance1year_z"

local tlist "EU EN UE NE EEjob_new"

local xcount = 1
foreach y of local tlist {

	*display " "
	*display in red " -------- `y'  all countries---------"

	if "`y'" == "UE" | "`y'" == "NE" {
		local xreg `xjfr'
	}
	if "`y'" == "EU" | "`y'" == "EN" {
		local xreg `xexit'
	}
	if "`y'" == "EEjob_new" {
		local xreg `xjj'
	}

	reg ytrate `xreg'  if trans == "`y'" & !missing(`xreg'), cluster(ccode)	
	sum ytrate if trans == "`y'"& !missing(`xreg')


	if `xcount' == 1 {
		#delimit ;
		outreg2 using "Figures/Table2_PanelAll.tex", replace
		keep(`xreg') 
		addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`y'")
		nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001. Regulatory variable measured as z-score.")
		;
		#delimit cr

	}
	else {
		#delimit ;
		outreg2 using "Figures/Table2_PanelAll.tex", append
		keep(`xreg') addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`y'")
		nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001. Regulatory variable measured as z-score.")
		;
		#delimit cr	
	}


	sleep 300

	display in red " -------- `y'  rich countries---------"	
	reg ytrate `xreg'_rich  if rich == 1 & trans == "`y'" & !missing(`xreg'_rich), cluster(ccode)
	sum ytrate if rich == 1 & trans == "`y'"& !missing(`xreg'_rich)

	if `xcount' == 1 {
		#delimit ;
		outreg2 using "Figures/Table2_PanelRich.tex", replace
		keep(`xreg'_rich) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`y'")
		nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001. Regulatory variable measured as z-score.")
		;
		#delimit cr
	}
	else {
		#delimit ;
		outreg2 using "Figures/Table2_PanelRich.tex", append
		keep(`xreg'_rich) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`y'")
		nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001. Regulatory variable measured as z-score.")
		;
		#delimit cr	
	}


	sleep 300

	local xcount = `xcount' + 1

}





